<template>
  <div class="main-layout">
    <header class="main-header">
      <div class="header-left">
        <div class="device-name">LWD800</div>
        <div class="header-divider"></div>
      </div>
      <nav class="main-nav">
        <button
          class="nav-btn"
          :class="{ active: activeTab === 'print' }"
          @click="activeTab = 'print'"
        >
          <PrinterOutlined class="nav-icon" />
        </button>
        <button
          class="nav-btn"
          :class="{ active: activeTab === 'control' }"
          @click="activeTab = 'control'"
        >
          <PlayCircleOutlined class="nav-icon" />
        </button>
        <button
          class="nav-btn"
          :class="{ active: activeTab === 'monitor' }"
          @click="activeTab = 'monitor'"
        >
          <VideoCameraOutlined class="nav-icon" />
        </button>
      </nav>
      <div class="header-user">
        <a-dropdown placement="bottomRight">
          <template #overlay>
            <a-menu>
              <a-menu-item @click="logout" key="logout" danger class="logout-menu-item">
                <LogoutOutlined class="logout-icon" />
                <span class="logout-text">退出登录</span>
              </a-menu-item>
            </a-menu>
          </template>
          <div class="user-dropdown-trigger">
            <a-avatar
              :size="56"
              style="background-color: #108ee9; font-size: 2rem; cursor: pointer"
            >
              {{ username?.charAt(0) || 'U' }}
            </a-avatar>
            <span class="user-name">{{ username }}</span>
            <DownOutlined class="user-arrow" />
          </div>
        </a-dropdown>
      </div>
    </header>
    <main class="main-content print-flex-layout" v-if="activeTab === 'print'">
      <div class="print-card print-card-left">
        <FileInfoPanel />
      </div>
      <div class="print-card print-card-center">
        <MonitorPanel />
      </div>
      <div class="print-card print-card-status">
        <a-card
          bordered
          style="
            height: 100%;
            width: 240px;
            min-width: 180px;
            max-width: 260px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
          "
        >
          <div class="device-info-list">
            <div class="device-info-item" v-for="item in deviceInfoList" :key="item.key">
              <div class="device-info-label">{{ item.label }}</div>
              <a-progress
                type="circle"
                :percent="item.percent"
                size="small"
                :strokeColor="item.color"
                :format="() => ''"
                :strokeWidth="6"
                :trailColor="'#f0f0f0'"
              />
              <div class="device-info-value">{{ item.value }}{{ item.unit }}</div>
            </div>
          </div>
        </a-card>
      </div>
    </main>
    <main v-else class="main-content">
      <div v-if="activeTab === 'control'">
        <h1>控制页面</h1>
        <div class="placeholder">此处为控制相关功能区</div>
      </div>
      <div v-else-if="activeTab === 'monitor'">
        <h1>监控页面</h1>
        <div class="placeholder">此处为监控相关功能区</div>
      </div>
    </main>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import {
  PrinterOutlined,
  PlayCircleOutlined,
  VideoCameraOutlined,
  DownOutlined,
  LogoutOutlined,
} from '@ant-design/icons-vue'
import { Progress as AProgress } from 'ant-design-vue'
import FileInfoPanel from '@/components/FileInfoPanel.vue'
import MonitorPanel from '@/components/MonitorPanel.vue'

const router = useRouter()
const userStore = useUserStore()
const username = computed(() => userStore.username)
const activeTab = ref<'print' | 'control' | 'monitor'>('print')
function logout() {
  userStore.logout()
  router.push('/login')
}

// 设备信息栏数据（模拟数据动态变化）
const deviceInfoList = ref([
  {
    key: 'pressure',
    label: '舱室压力',
    value: 32.5,
    unit: 'kPa',
    percent: 65,
    color: '#108ee9',
    min: 0,
    max: 50,
  },
  {
    key: 'temp',
    label: '基板温度',
    value: 420,
    unit: '℃',
    percent: 84,
    color: '#faad14',
    min: 0,
    max: 500,
  },
  {
    key: 'o2',
    label: '氧含量',
    value: 12,
    unit: 'ppm',
    percent: 12,
    color: '#52c41a',
    min: 0,
    max: 100,
  },
  {
    key: 'filterO2',
    label: '过滤器氧含量',
    value: 8,
    unit: 'ppm',
    percent: 8,
    color: '#eb2f96',
    min: 0,
    max: 100,
  },
  {
    key: 'wind',
    label: '风速',
    value: 2.3,
    unit: 'm/s',
    percent: 46,
    color: '#722ed1',
    min: 0,
    max: 5,
  },
])

// 模拟数据动态变化
onMounted(() => {
  setInterval(() => {
    deviceInfoList.value = deviceInfoList.value.map((item) => {
      // 随机微调数值
      let newValue = item.value + (Math.random() - 0.5) * (item.max - item.min) * 0.02
      newValue = Math.max(item.min, Math.min(item.max, newValue))
      // 百分比自动计算
      const percent = Math.round(((newValue - item.min) / (item.max - item.min)) * 100)
      return { ...item, value: parseFloat(newValue.toFixed(2)), percent }
    })
  }, 2000)
})
</script>

<style scoped>
.main-layout {
  width: 100vw;
  height: 100vh;
  background: #f5f6fa;
  display: flex;
  flex-direction: column;
}
.main-header {
  width: 100vw;
  min-width: 0;
  height: 110px;
  background: #1a2233;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 2vw;
  box-sizing: border-box;
  gap: 1vw;
}
.header-left {
  display: flex;
  align-items: center;
  height: 100%;
}
.device-name {
  font-size: 2.6rem;
  font-weight: bold;
  color: #fff;
  letter-spacing: 2px;
  margin-right: 2.5vw;
}
.header-divider {
  width: 2px;
  height: 60px;
  background: #3a4255;
  margin-right: 4vw;
  border-radius: 2px;
}
.main-nav {
  flex: 1;
  min-width: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 3vw;
  overflow-x: auto;
}
.nav-btn {
  width: 90px;
  height: 90px;
  background: #fff;
  border: none;
  border-radius: 1.2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.8rem;
  color: #1a2233;
  box-shadow: 0 2px 12px 0 rgba(31, 38, 135, 0.08);
  transition:
    background 0.2s,
    color 0.2s;
  cursor: pointer;
}
.nav-btn.active,
.nav-btn:hover {
  background: #108ee9;
  color: #fff;
}
.nav-icon {
  font-size: 2.8rem;
}
.header-user {
  flex-shrink: 0;
  min-width: 120px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-right: 3vw;
}
.user-dropdown-trigger {
  display: flex;
  align-items: center;
  gap: 1.2rem;
  cursor: pointer;
  padding: 0.5rem 1.2rem;
  border-radius: 1.2rem;
  background: rgba(255, 255, 255, 0.06);
  transition: background 0.2s;
}
.user-dropdown-trigger:hover {
  background: rgba(16, 142, 233, 0.12);
}
.user-name {
  color: #fff;
  font-size: 1.7rem;
  font-weight: 500;
  margin-left: 0.5rem;
  margin-right: 0.5rem;
  user-select: none;
}
.user-arrow {
  color: #fff;
  font-size: 1.5rem;
}
.main-content {
  flex: 1;
  width: 100vw;
  background: #f5f6fa;
  overflow: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-bottom: 96px;
}
.placeholder {
  margin-top: 2.5rem;
  color: #888;
  font-size: 1.5rem;
}
.logout-menu-item {
  font-size: 2rem !important;
  line-height: 3.5rem !important;
  padding: 1.2rem 2.5rem !important;
  display: flex !important;
  align-items: center !important;
  justify-content: flex-start !important;
  min-width: 180px;
  user-select: none;
}
.logout-icon {
  font-size: 2.2rem !important;
  margin-right: 1.2rem;
}
.logout-text {
  font-size: 2rem;
  font-weight: bold;
  letter-spacing: 2px;
}
/* 右侧设备信息栏样式 */
.device-info-sidebar {
  position: fixed;
  top: 120px; /* 顶部留白，避免遮挡导航栏 */
  right: 16px; /* 紧贴右侧但不超出 */
  bottom: 16px; /* 底部留白，避免超出 */
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 2px 16px 0 rgba(31, 38, 135, 0.1);
  padding: 18px 10px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  min-width: 90px;
  max-height: calc(100vh - 136px); /* 120(top) + 16(bottom) */
  overflow-y: auto;
}
.device-info-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  width: 80px;
  margin: 0;
}
.device-info-label {
  font-size: 0.95rem;
  color: #666;
  margin-bottom: 2px;
  text-align: center;
  white-space: nowrap;
  font-weight: 500;
  letter-spacing: 0.5px;
}
.device-info-value {
  font-size: 1.1rem;
  font-weight: bold;
  color: #222;
  margin-top: 2px;
  text-align: center;
  white-space: nowrap;
}
:deep(.ant-progress-circle) {
  font-size: 1rem;
}
.print-flex-layout {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  justify-content: flex-start;
  width: 100vw;
  height: calc(100vh - 110px - 24px);
  background: #f5f6fa;
  gap: 24px;
  box-sizing: border-box;
  padding: 12px 8px 12px 0;
  overflow-x: auto;
}
.print-card-left,
.print-card-center {
  width: calc((100vw - 27px * 2 - 110px) / 2.3);
  min-width: 0;
  display: flex;
  flex-direction: column;
  height: 100%;
}
.print-card-status {
  width: 96px;
  min-width: 80px;
  max-width: 110px;
  flex-shrink: 0;
  align-items: stretch;
  justify-content: flex-start;
  display: flex;
  flex-direction: column;
  height: 100%;
}
.device-info-list {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  width: 100%;
}
@media (max-width: 1200px) {
  .main-header {
    height: auto;
    padding: 0 1vw;
    gap: 0.5vw;
  }
  .main-nav {
    gap: 1vw;
  }
  .nav-btn {
    width: 60px;
    height: 60px;
    font-size: 1.6rem;
  }
  .device-name {
    font-size: 1.5rem;
  }
  .header-divider {
    height: 40px;
    margin-right: 2vw;
  }
  .user-name {
    font-size: 1.1rem;
  }
  .user-dropdown-trigger {
    padding: 0.3rem 0.7rem;
    gap: 0.7rem;
  }
}
@media (max-width: 700px) {
  .main-header {
    flex-direction: column;
    height: auto;
    padding: 0.5rem 0.5rem;
    gap: 0.5rem;
  }
  .main-nav {
    width: 100%;
    justify-content: center;
    gap: 0.5rem;
  }
  .header-left,
  .header-user {
    width: 100%;
    justify-content: center;
    font-size: 1rem;
  }
  .user-dropdown-trigger {
    width: 100%;
    justify-content: center;
  }
}
@media (max-width: 900px) {
  .device-info-sidebar {
    right: 4px;
    padding: 8px 2px;
    gap: 8px;
    min-width: 56px;
  }
  .device-info-item {
    width: 48px;
  }
  .device-info-label {
    font-size: 0.7rem;
  }
  .device-info-value {
    font-size: 0.8rem;
  }
}
</style>
